﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AttTypeDefine;

public class GlobalHelper
{
    public static PianoGame pianoGame;
    public static EGameType EGameType;
    public static bool IsTurn=true;
    public static AudioSource backGround;

    #region 主游戏
    static Game1 maingame;
    public static Game1 MainGame
    {
        get
        {
            if (null == maingame)
            {
                maingame = GameObject.Find("Game1").GetComponent<Game1>();
            }
            return maingame;
        }
    }
    #endregion

    #region  通过名字递归找子对象
    public static GameObject FindGameObjectWithName(GameObject obj, string name)
    {
        if (null == obj)
            return null;
        GameObject target = null;
        if (obj.name == name)
            return obj;
        for (int i = 0; i < obj.transform.childCount; i++)
        {
            if (obj.transform.GetChild(i).name == name)
                return obj.transform.GetChild(i).gameObject;
            else
            {
                target = FindGameObjectWithName(obj.transform.GetChild(i).gameObject, name);

                if (null != target && target.name == name)
                    return target;
            }
        }
        return target;
    }
    #endregion

    #region UI管理
    static UIMgr uimgr;
    public static UIMgr UIMGR
    {
        get
        {
            if (null == uimgr)
            {
                GameObject obj = GameObject.FindGameObjectWithTag("UIMgr");
                if (null == obj)
                {
                    obj = new GameObject("UIMgr")
                    {
                        tag = "UIMgr"
                    };
                }
                uimgr = obj.GetOrAddComponent<UIMgr>();
            }
            return uimgr;
        }
    }
    #endregion

    #region 画布
    static Canvas canvas;
    public static Canvas CANVAS
    {
        get
        {
            if(null ==canvas)
            {
                canvas = UIMGR.m_oCANVAS.GetComponent<Canvas>();
            }
            return canvas;
        }
    }
    public static Vector3 WorldToUIPoint(Transform worldGo)
    {
        Vector3 v_v3 = Camera.main.WorldToScreenPoint(worldGo.position);
        Vector3 v_ui = CANVAS.worldCamera.ScreenToWorldPoint(v_v3);
        Vector3 v_new = new Vector3(v_ui.x, v_ui.y, CANVAS.GetComponent<RectTransform>().anchoredPosition3D.z);
        return v_new;
    }
    #endregion

    #region 监听
    static GameObject eventsystem;
    public static GameObject Event
    {
        get
        {
            if (null == eventsystem)
                eventsystem = GameObject.Find("EventSystem");
            return eventsystem;
        }
    }
    #endregion

    #region 对象池
    static SpawnPool spherepool;
    public static SpawnPool SpherePool
    {
        get
        {
            if (null == spherepool)
            {
                spherepool = CreatePool("Sphere", MainGame.transform);
                spherepool.gameObject.AddComponent<SphereTranslateMgr>();
            }
            return spherepool;
        }
    }
    static SpawnPool CreatePool(string name,Transform parent)
    {
        string poolname = name;
        poolname = poolname.Replace("Pool", "");
        GameObject obj = new GameObject(poolname + "Pool");
        Transform t = obj.transform;
        t.parent=parent;
        t.position = Vector3.zero;
        t.rotation = Quaternion.identity;
        t.localScale = Vector3.one;
        SpawnPool pool = obj.AddComponent<SpawnPool>();
        return pool;
    }
    #endregion

    #region UI相机
    private static Camera uicam;
    public static Camera UICam
    {
        get
        {
            if (null == uicam)
                uicam = CANVAS.worldCamera;
                //uicam = FindGameObjectWithName(UIMGR.m_oCANVAS,"Camera").GetComponent<Camera>();
            return uicam;
        }
    }
    #endregion

    #region 当前游戏类型
    private static EPianoType nowgametype;
    public static EPianoType NowGameType
    {
        get
        {
            return nowgametype;
        }
        set
        {
            if (value != nowgametype)
                nowgametype = value;
        }
    }
    #endregion
}
